import { login, loginAdmin, loginMerc, logout, getInfo, loginCompany, backAdminauthApiloginbind, backAdminauthApilogin } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import defAva from '@/assets/images/profile.jpg'

const useUserStore = defineStore(
  'user',
  {
    state: () => ({
      token: getToken(),
      id: '',
      name: '',
      avatar: '',
      roles: [],
      permissions: [],
      type: "",
      // 
      merchant: {},
      areaId: '',
      industryId: '',
    }),
    actions: {
      /* 新登录绑定 */
      backAdminauthApiloginbindApi(userInfo) {
        const phone = userInfo.phone
        const merchantId = userInfo.merchantId
        return new Promise((resolve, reject) => {
          backAdminauthApiloginbind({
            phone: phone,
            merchantId: merchantId
          }).then(res => {
            console.log(res,
              "res login backAdminauthApiloginbindApi"
            );
            
            setToken(res.token)
            this.token = res.token
            resolve(res)
          }).catch(error => {
            reject(error)
          })
        })
      },
      /* 新登录 */
      backAdminauthApiloginApi(userInfo) {
        console.log('444', userInfo);
        const phone = userInfo.phone
        return new Promise((resolve, reject) => {
          backAdminauthApilogin({
            phone: phone
          }).then(res => {
            console.log(res, "res login backAdminauthApiloginbindApi");
            // setToken(res.token)
            // this.token = res.token
            resolve(res)
          }).catch(error => {
            reject(error)
          })
        })
      },
      // 登录
      login(userInfo) {
        const username = userInfo.username.trim()
        const password = userInfo.password
        const code = userInfo.code
        const uuid = userInfo.uuid
        return new Promise((resolve, reject) => {
          login(username, password, code, uuid).then(res => {
            setToken(res.token)
            this.token = res.token
            resolve()
          }).catch(error => {
            reject(error)
          })
        })
      },
      loginAdmin(userInfo) {
        // console.log(userInfo, "userInfo loginAdmin");
        const username = userInfo.username
        const password = userInfo.password
        const code = userInfo.code
        const uuid = userInfo.uuid
        const phone = userInfo.phone
        return new Promise((resolve, reject) => {
          loginAdmin(username, password, code, uuid, phone).then(res => {
            setToken(res.token)
            this.token = res.token
            resolve()
          }).catch(error => {
            reject(error)
          })
        })
      },
      loginMerc(userInfo) {
        // const username = userInfo.username.trim()
        const username = userInfo.username
        const password = userInfo.password
        const code = userInfo.code
        const uuid = userInfo.uuid
        const merchantId = userInfo.merchantId
        const phone = userInfo.phone
        return new Promise((resolve, reject) => {
          // loginMerc(username, password, code, uuid,merchantId,phone).then(res => {
          loginMerc({ phone, merchantId }).then(res => {
            setToken(res.token)
            this.token = res.token
            resolve()
          }).catch(error => {
            reject(error)
          })
        })
      },
      loginCompany(userInfo) {
        // const username = userInfo.username.trim()
        const username = userInfo.username
        const password = userInfo.password
        const code = userInfo.code
        const uuid = userInfo.uuid
        const merchantId = userInfo.merchantId
        const phone = userInfo.phone
        return new Promise((resolve, reject) => {
          // loginMerc(username, password, code, uuid,merchantId,phone).then(res => {
          loginCompany({ phone, merchantId }).then(res => {
            setToken(res.token)
            this.token = res.token
            resolve()
          }).catch(error => {
            reject(error)
          })
        })
      },

      // 获取用户信息
      getInfo() {
        return new Promise((resolve, reject) => {
          getInfo().then(res => {
            // console.log(res, "res111");

            const user = res.user
            const areaId = res.areaId
            const industryId = res.industryId
            const type = res.type
            // const avatar = (user.avatar == "" || user.avatar == null) ? defAva : import.meta.env.VITE_APP_BASE_API + user.avatar;
            const avatar = (user.avatar == "" || user.avatar == null) ? defAva : user.avatar;

            if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组
              this.roles = res.roles
              this.permissions = res.permissions

            } else {
              this.roles = ['ROLE_DEFAULT']
            }
            // 判断是否打开弹框状态
            this.type = type

            this.merchant = res.merchant
            this.id = user.userId
            this.name = user.userName
            this.avatar = avatar
            this.industryId = industryId
            this.areaId = areaId

            resolve(res)
          }).catch(error => {
            reject(error)
          })
        })
      },
      // 退出系统
      logOut() {
        return new Promise((resolve, reject) => {
          logout(this.token).then(() => {
            this.token = ''
            this.roles = []
            this.permissions = []
            // 
            this.merchant = {}
            this.areaId = ''
            this.industryId = ''
            removeToken()
            resolve()
          }).catch(error => {
            reject(error)
          })
        })
      }
    }
  })

export default useUserStore
